
package com.elong.common.http;

import java.util.List;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.ClientCookie;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.impl.cookie.BasicClientCookie2;



/**
 * @ClassName: CookieUtil

 * @Description: cookie管理工具

 * @author Guanguo.Gao

 * @date 2014年9月11日 下午3:09:00

 * @version V1.0
 */
public class CookieUtil {

    //re create a netscape cookie
    public static void createNetscapeCookie(){
        BasicClientCookie netscapeCookie = new BasicClientCookie("name", "value");
        netscapeCookie.setVersion(0);
        netscapeCookie.setDomain(".elong.com");
        netscapeCookie.setPath("/");
    }
    
    //re creating a standard cookie
    public static void createStandaloneCookie(){
        BasicClientCookie stdCookie = new BasicClientCookie("name", "value");
        stdCookie.setVersion(1);
        stdCookie.setDomain(".elong.com");
        stdCookie.setPath("/");
        stdCookie.setSecure(true);
        //set attributes exactly as sent by the server
        stdCookie.setAttribute(ClientCookie.VERSION_ATTR, "1");
        stdCookie.setAttribute(ClientCookie.DOMAIN_ATTR, ".elong.com");
    }
    
    //create a set-Cookie2
    public static void createCookie2(){
        BasicClientCookie2 stdCookie2 = new BasicClientCookie2("name", "value");
        stdCookie2.setVersion(1);
        stdCookie2.setDomain(".elong.com");
        stdCookie2.setPorts(new int[]{80, 8080});
        stdCookie2.setPath("/");
        stdCookie2.setSecure(true);
        stdCookie2.setAttribute(ClientCookie.VERSION_ATTR, "1");
        stdCookie2.setAttribute(ClientCookie.DOMAIN_ATTR, ".elong.com");
        stdCookie2.setAttribute(ClientCookie.PORT_ATTR, "80,8080");
    }
    
    /**
     * @Description: 遍历cookie
     * @param store
     * @throws
     */
    public static void queryCookieStore(CookieStore store){
        List<Cookie> cookies = store.getCookies();
        for(Cookie cookie : cookies){
            System.out.println("Local cookie:" + cookie);
        }
    }
}
